/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugBuyPlan;
import com.jims.master.modules.drug.entity.DrugInfo;
import com.jims.master.modules.drug.entity.DrugSupplierCata;
import com.jims.master.modules.drug.service.DrugBuyPlanService;
import com.jims.master.modules.drug.service.DrugSupplierCataService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 药品采购计划审核Controller
 * @author DT
 * @version 2017-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/buyPlanAudit")
public class BuyPlanAuditController extends BaseController {

	@Autowired
	private DrugBuyPlanService drugBuyPlanService;
	@Autowired
	private DrugSupplierCataService drugSupplierCataService;
	@ModelAttribute
	public DrugBuyPlan get(@RequestParam(required=false) String id) {
		DrugBuyPlan entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugBuyPlanService.get(id);
		}
		if (entity == null){
			entity = new DrugBuyPlan();
		}
		return entity;
	}

    /**
     * 采购计划生成首页
     * @param drugBuyPlan
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"createPlanIndex",""})
    public String index(DrugBuyPlan drugBuyPlan,HttpServletRequest request,HttpServletResponse response,Model model){
        drugBuyPlan.setStateFlag(null);
        Page<DrugBuyPlan> page = drugBuyPlanService.findPage(new Page<DrugBuyPlan>(request, response), drugBuyPlan);
        model.addAttribute("page", page);
        return "modules/drug/drugBuyPlanCreateIndex";
    }

    /**
     * 采购计划生成，药品明细
     * @param drugBuyPlan
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"createPlanForm",""})
    public String form(DrugBuyPlan drugBuyPlan,HttpServletRequest request,HttpServletResponse response,Model model){
        model.addAttribute("drugBuyPlan",drugBuyPlan);
        return "modules/drug/drugBuyPlanCreateForm";
    }

    /**
     * 保存采购计划(生成)
     * @param drugBuyPlan
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveDrugBuyPlanCreate")
    @ResponseBody
    public StringData saveDrugBuyPlanCreate(DrugBuyPlan drugBuyPlan,Model model){
        StringData data = new StringData();
        int num = drugBuyPlanService.saveDrugBuyPlanCreate(drugBuyPlan);
        if (num > 0) {
            data.setCode("success");
            data.setData("保存成功");
        } else {
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
    }

    /**
     * 自动生成采购计划药品名称补全
     * @param q
     * @param limit
     * @param drugInfo
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoBuyPlanDrugInfo", ""})
    @ResponseBody
    public List<DrugBuyPlan> autoBuyPlanDrugInfo(String q, String limit, DrugInfo drugInfo) {
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<DrugBuyPlan> list = drugBuyPlanService.autoBuyPlanDrugInfo(drugInfo, UserUtils.getOrgId(), q, limit);
        return list;
    }

    /**
     * 采购计划调整首页
     * @param drugBuyPlan
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"updatePlanIndex",""})
    public String updatePlanIndex(DrugBuyPlan drugBuyPlan,HttpServletRequest request,HttpServletResponse response, Model model){
        if(null != drugBuyPlan && null != drugBuyPlan.getPurchaseNumber()){
            drugBuyPlan.setStateFlag("0");
            Page<DrugBuyPlan> page = drugBuyPlanService.getDrugPlanList(drugBuyPlan);
            model.addAttribute("page", page);
        }else{
            drugBuyPlan.setDelFlag("1");
            Page<DrugBuyPlan> page = drugBuyPlanService.getDrugPlanList(drugBuyPlan);
            model.addAttribute("page", page);
        }
        //采购供应商
        List<DrugSupplierCata> list = drugSupplierCataService.findList(new DrugSupplierCata()) ;
        model.addAttribute("cataList", list);
        return "modules/drug/drugBuyPlanUpdateIndex";
    }

    /**
     * 调整采购计划，增加药品明细
     * @param drugBuyPlan
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"updatePlanForm", ""})
    public String updateDrugBuyPlanForm(DrugBuyPlan drugBuyPlan, HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("drugBuyPlan", drugBuyPlan);
        return "modules/drug/drugBuyPlanUpdateForm";
    }

    /**
     * 保存采购计划调整数据
     * @param drugBuyPlan
     * @return
     * fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"saveUdatePlan", ""})
    @ResponseBody
    public StringData save(DrugBuyPlan drugBuyPlan) {
        return drugBuyPlanService.saveOrUpdateDrugBuyPlan(drugBuyPlan,"1");
    }

    /**
     * 保存采购计划审核数据
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"saveDrugPlanAudit",""})
    @ResponseBody
    public StringData saveDrugPlanAudit(DrugBuyPlan drugBuyPlan){
        return drugBuyPlanService.saveOrUpdateDrugBuyPlan(drugBuyPlan, "2");
    }

    /**
     * 采购计划调整首页
     * @param drugBuyPlan
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugBuyPlan drugBuyPlan, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (null != drugBuyPlan && null != drugBuyPlan.getPurchaseNumber()) {
            drugBuyPlan.setStateFlag("1");
            Page<DrugBuyPlan> page = drugBuyPlanService.getDrugPlanList(drugBuyPlan);
            model.addAttribute("page", page);
        } else {
            drugBuyPlan.setDelFlag("2");
            Page<DrugBuyPlan> page = drugBuyPlanService.getDrugPlanList(drugBuyPlan);
            model.addAttribute("page", page);
        }
        //采购供应商
        List<DrugSupplierCata> list = drugSupplierCataService.findList(new DrugSupplierCata());
        model.addAttribute("cataList", list);
		return "modules/drug/buyPlanAuditIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugBuyPlan drugBuyPlan, Model model) {
		DrugSupplierCata drugSupplierCata=new DrugSupplierCata();
		List<DrugSupplierCata> list=drugSupplierCataService.findList(drugSupplierCata);
		model.addAttribute("list",list);
		model.addAttribute("drugBuyPlan", drugBuyPlan);
		return "modules/drug/buyPlanAuditForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugBuyPlan drugBuyPlan, Model model) {
		drugBuyPlan.setStateFlag("2");
	    StringData data=new StringData();
		int i=drugBuyPlanService.save(drugBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("审核成功");
        }else{
            data.setCode("error");
            data.setData("审核失败");
        }
        return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(HttpServletRequest request,HttpServletResponse response,@RequestParam("id")String id) {
	    StringData data=new StringData();
        DrugBuyPlan drugBuyPlan = new DrugBuyPlan();
        drugBuyPlan.setId(id);
		int i=drugBuyPlanService.delete(drugBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}